package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.template.BuildTemplateV2;
import com.xcity.game.item.ItemTemplate;

import naga.x.App;
import naga.x.game.template.TemplateService;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class BuildListV2Response extends Response {
	
	public List<BuildV2Response> list;
	public List<MaterialTemplate> items; // 材料模板
	
	private static List<MaterialTemplate> MATERIALS;
	static {
		TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
		MATERIALS = Arrays.asList(
				new MaterialTemplate(service.get(ItemTemplate.class, BuildTemplateV2.PART3_ITEM_ID)),
				new MaterialTemplate(service.get(ItemTemplate.class, BuildTemplateV2.PART5_ITEM_ID)),
				new MaterialTemplate(service.get(ItemTemplate.class, BuildTemplateV2.PART2_ITEM_ID)),
				new MaterialTemplate(service.get(ItemTemplate.class, BuildTemplateV2.PART1_ITEM_ID)),
				new MaterialTemplate(service.get(ItemTemplate.class, BuildTemplateV2.PART4_ITEM_ID))
		);
	}
	
	// don't serialize the code
	public static class BuildV2Info extends BuildV2Response {
		public BuildV2Info(Player player, BuildV2 building) {
			super(-1, player, building);
			this.parts = null; // no need parts
		}
		public BuildV2Info(Player player, BuildTemplateV2 template) {
			super(-1, player, template);
		}
		@JSONField(serialize = false)
		@Override
		public short getCode() {
			return super.getCode();
		}
	}
	
	// 材料模板信息
	public static class MaterialTemplate {
		public int id;
		public String name;
		public String icon;
		public String desc;
		public MaterialTemplate(ItemTemplate template) {
			this.id = template.getId();
			this.name = template.getName();
			this.icon = template.getIcon();
			this.desc = template.getDesc();
		}
	}
	
	public BuildListV2Response(int serial, Player player, List<BuildTemplateV2> templates) {
		super(serial);
		List<BuildV2> buildings = player.getBuildings2().getBuildings();
		list = new ArrayList<>(templates.size());
		int offset = 0;
		for (int size = buildings.size(); offset < size; offset++) {
			BuildV2 building = buildings.get(offset);
			list.add(new BuildV2Info(player, building));
		}
		for (int size = templates.size(); offset < size; offset++) {
			BuildTemplateV2 template = templates.get(offset);
			list.add(new BuildV2Info(player, template));
		}
		items = MATERIALS;
	}

}
